package com.example.rabbitmq.controller.delay;

import com.example.rabbitmq.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

//@Component
public class DelayQueueListener {
    @RabbitListener(queues = Constants.DELAY_QUEUE)
    public void listenerQueue(Message message, Channel channel) throws IOException {
        try {
            System.out.printf("接收到的消息:%s, diveryTag:%d, time:%s\n",new String(message.getBody()),
                    message.getMessageProperties().getDeliveryTag(),new Date());
//            int a = 1 / 0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }
    }
}
